﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Entities;
using OreSystem.BLL;
namespace MainFrom {
    public partial class UserEditFrom : Form {
        private UserBLL userBll = new UserBLL();
        public User NewUser { get; set; }
        public bool NewUserFlag = true;
        public UserEditFrom() {
            InitializeComponent();
        }

        private void UserEditFrom_Load(object sender, EventArgs e) {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            
            cmb_Role.Items.Clear();
            cmb_Role.Items.Add("管理员");
            cmb_Role.Items.Add("用  户");
            cmb_Role.SelectedIndex = 1;

            lbl_msg.ForeColor = Color.Red;

            txt_PassWord.PasswordChar = '*';
            if (NewUser.ID > 0) {
                NewUserFlag = false;
                txt_Name.Text = NewUser.UserName;
                txt_PassWord.Text = NewUser.PassWord;
                txt_PassWord.Enabled = false;
                cmb_Role.SelectedIndex = NewUser.Role;
            }
        }

        private void btn_save_Click(object sender, EventArgs e) {
            string name = txt_Name.Text;
            string pw = txt_PassWord.Text;
            if (string.IsNullOrEmpty(name)) {
                lbl_msg.Text = "用户名不能为空";
                txt_Name.Focus();
                return;
            }
            if (NewUserFlag&&userBll.GetUserByUserName(name) !=null) {
                lbl_msg.Text = "用户已经存在！";
                txt_Name.Focus();
                return;
            }
            if (string.IsNullOrEmpty(pw)) {
                lbl_msg.Text = "密码不能为空";
                txt_PassWord.Focus();
                return;
            }
            try {
                NewUser.UserName = name;
                NewUser.PassWord = pw;
                NewUser.Role = cmb_Role.SelectedIndex;
                NewUser.CreateDate = DateTime.Now;
                userBll.Save(NewUser);
                if (NewUserFlag) {
                    lbl_msg.Text = "添加成功，请继续添加！";
                    Clear();
                }
                else {
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex) {
                MessageBox.Show("程序出错请联系管理员，错误提示"+ex.Message.ToString());               
            }
            
        }
        void Clear() {
            txt_Name.Text = "";
            txt_PassWord.Text = "";
            txt_Name.Focus();
        }

        private void btn_reset_Click(object sender, EventArgs e) {
            Clear();
        }             

        private void UserEditFrom_FormClosing(object sender, FormClosingEventArgs e) {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
     }
}
